# -*- coding: utf-8 -*-
import os
import hou
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Houdini
from liberLibs.utils import read_json


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        f = self.file
        if not os.path.isfile(f):
            self.append_error("%s is not an exist file." % f)
            return
        Houdini.install_hda(f)
        for i in [self.create_in_network, self.create_obj, self.create_geo,
                  self.create_mat, self.create_out, self.create_shop]:
            try:
                node = i()
                if node:
                    node.setCurrent(1)
                    return
            except:
                pass
        self.show_path()

    def create_in_network(self):
        current_pane = Houdini.get_current_network_editor_pane()
        current_node = current_pane.pwd()
        node = current_node.createNode(self.name)
        return node

    def create_obj(self):
        hda_node = hou.node("obj").createNode(self.name)
        return hda_node

    def create_geo(self):
        node = hou.node("obj").createNode("geo")
        hou.node("%s/file1" % node.path()).destroy()
        try:
            hda_node = node.createNode(self.name)
            return hda_node
        except:
            node.destroy()
            return False

    def create_mat(self):
        node = hou.node("mat").createNode(self.name)
        return node

    def create_out(self):
        node = hou.node("out").createNode(self.name)
        return node

    def create_shop(self):
        node = hou.node("shop").createNode(self.name)
        return node

    def show_path(self):
        json_path = "%s/path.json" % self.location
        json_path = json_path.replace("\\", "/")
        data = read_json(json_path)
        if not data:
            return
        category = data.get("category")
        path = data.get('path')
        if category and path:
            msg = "%s\ncategory:  %s\n\n%spath:  %s\n%s" % ("-"*70, category, " "*9, path, "-"*70)
            hou.ui.displayMessage(msg)
